package post.graduate.go.ready.metasearch;


import java.util.ArrayList;
import java.util.List;

import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTML.Tag;
import javax.swing.text.html.HTMLEditorKit.ParserCallback;

public class SearchParser extends ParserCallback {

	private boolean isTd = false;
	
	private boolean isDefaultTable = false;
	
	private boolean titleFinished = false;
	
	private boolean urlFinnished = false;
	
	private String title;
	
	private String content;
	
	private int brCount = 0;
	
	private List<String> titleList = new ArrayList<String>();
	
	private List<String> contentList = new ArrayList<String>();
	
	private List<String> urlList = new ArrayList<String>();
	
	public List<String> getTitleList() {
		return titleList;
	}
	
	public List<String> getContentList() {
		return contentList;
	}

	public List<String> getUrlList() {
		return urlList;
	}
	

	@Override
	public void handleEndTag(Tag t, int pos) {
		if (t == HTML.Tag.TABLE) {
			if (isDefaultTable) {
				isDefaultTable = false;
				titleFinished = false;
				urlFinnished = false;
				brCount = 0;
				title = "";
				content = "";
			}
		} else if (t == HTML.Tag.TD) {
			if (isTd) {
				isTd = false;
			}
		} 
	}

	@Override
	public void handleStartTag(Tag t, MutableAttributeSet a, int pos) {
		if(t == HTML.Tag.TABLE)
		{
			String attribute = (String)a.getAttribute(HTML.Attribute.ID);
			//System.out.println(attribute);
			try {
				Integer.parseInt(attribute);
				isDefaultTable = true;
			} catch(NumberFormatException ignore) {
				// do nothing
			}
			
		} else if(t == HTML.Tag.TD && isDefaultTable) {
			isTd = true;
			titleFinished = false;
		} 
	}

	@Override
	public void handleSimpleTag(Tag t, MutableAttributeSet a, int pos) {
		if(t == HTML.Tag.BR && isTd) {
			titleFinished = true;
			brCount++;
			if (isTd && isDefaultTable && brCount == 1) {
				if (title != null) {
					titleList.add(title);
				}
			} else if (isTd && isDefaultTable && brCount == 2) {
				if (content != null) {
					contentList.add(content);
				}
			} 
		} 
	}

	@Override
	public void handleText(char[] data, int pos) {
		if (isTd && isDefaultTable && brCount <= 0 && !titleFinished) {
			String tmptitle = new String(data);
			if (tmptitle != null) {
				if (title == null)
					title = tmptitle;
				else
					title += tmptitle;
			}
		} else if (isTd && isDefaultTable && brCount == 1) {

			String tmpcontent = new String(data);
			if (tmpcontent != null) {
				if (content == null)
					content = tmpcontent;
				else
					content += tmpcontent;
			}
		} else if (isTd && isDefaultTable && !urlFinnished && brCount == 2 ) {
			String url = new String(data);
			if (url != null) {
				int spaceIndex = url.indexOf(" ") == -1 ? url.length() : url.indexOf(" ");
				int andspaceIndex = url.indexOf("&nbsp;") == -1 ? url.length() : url.indexOf("&nbsp;");
				int length = spaceIndex >= andspaceIndex ? andspaceIndex : spaceIndex;
				urlList.add(url.substring(0, length));
				urlFinnished = true;
			}
		}
	}

	
	

}
